<?php
namespace Admin\Model;
use Think\Model;
/*
 *邮寄管理 
 */
class DeliveryModel extends Model {
	//首页
	public function index($arr,$m_psize){
		$psize    = $arr['psize'];
		$psize    = (!$psize || $psize > $m_psize ) ? $m_psize : $psize ;
		
		//搜索信息检索
		if(!empty($arr['customer'])){
			$customer_ = $arr['customer'];
			$maps['customer.nickname'] = array('like','%'.$customer_.'%');
			$maps['customer.phone']    = array('like','%'.$customer_.'%');
			$maps['customer.customer_code'] = array('like','%'.$customer_.'%');
			$maps['_logic']   = 'OR';
			$wheres['_complex'] = $maps;
		}
		if(!empty($arr['sales'])){
			$saless = $arr['sales'];
			$wheres['user.nickname'] = array('like','%'.$saless.'%');
		}
		if(!empty($arr['order_num'])){
			$wheres['delivery.order_num']    = $arr['order_num'];
		}

		//区间与时间检索
		$stime    = I('get.first_time','','trim');
		$etime    = I('get.second_time','','trim');
		if( $stime && $etime ){
			$stime = date('Y-m-d 00:00:00',strtotime($stime));
			$etime = date('Y-m-d 23:59:59', strtotime($etime));
			if(strtotime($stime) < strtotime($etime)) $wheres['delivery.create_time'] = array('between',array($stime,$etime));
			else $wheres['delivery.create_time'] = array('between',array($etime,$stime));
		}elseif($stime  && !$etime ){
			$stime = date('Y-m-d 00:00:00',strtotime($stime));
			$wheres['delivery.create_time'] =  array('egt',$stime);
		}elseif(!$stime && $etime ){
			$etime = date('Y-m-d 23:59:59', strtotime($etime));
			$wheres['delivery.create_time'] = array('elt', $etime);
		}
		$wheres['delivery.source_type'] = $arr['matter'];
		//标签条件检索
		if($arr['matter_key'] == 6){
			$wheres['delivery.status'] = array('between',array('1','60'));
			$sequence = "delivery.create_time desc";
		}else{
			$wheres['delivery.status'] = $arr['matter_key'];
			if($arr['matter_key'] == 30 || $arr['matter_key'] == 40){
				$sequence = "delivery.create_time desc";
			}else{
				$sequence = "delivery.create_time desc";
			}
		}
		//首页列表信息
		$Delivery     = M("delivery");
		$count = $Delivery
				->join("LEFT JOIN customer ON delivery.customer_id = customer.customer_id")
				->join("LEFT JOIN contract ON delivery.source_id   = contract.contract_id")
				->join("LEFT JOIN product  ON contract.product_id  = product.product_id")
				->join("LEFT JOIN user     ON delivery.sales_id    = user.user_id ")
		        ->where($wheres)->count();
		$Page = new \Common\ORG\Page( $count, $psize );
		$list = $Delivery
					->field("customer.customer_id,customer.customer_code,customer.nickname,customer.mount,customer.remark,delivery.delivery_id,delivery.recipients,delivery.phone,delivery.address,delivery.customer_id,delivery.source_id,delivery.source_type,delivery.source_status,delivery.order_num,delivery.delivery_num,delivery.send_way,delivery.sales_id,delivery.sales_sector,delivery.remark as delivery_remrk,delivery.sales_manager,delivery.sales_subcompany,delivery.send_time,delivery.reach_time,delivery.status,delivery.is_export,delivery.create_time
						,contract.mount as contract_mount,contract.start_time,contract.state,contract.sign_status,contract.contract_id,contract.end_time,contract.weigui_status,product.name")
					->join("LEFT JOIN customer ON delivery.customer_id = customer.customer_id")
					->join("LEFT JOIN contract ON delivery.source_id   = contract.contract_id")
					->join("LEFT JOIN product  ON contract.product_id  = product.product_id")
					->join("LEFT JOIN user     ON delivery.sales_id    = user.user_id ")
                    ->where($wheres)
                    ->order($sequence )
                    ->limit($Page->firstRow.','.$Page->listRows )
		            ->select();

		//客户信息、截取时间为 Y-m-d
		foreach ($list as &$value) { 
			$value['create_time']  = date('Y-m-d',strtotime($value['create_time']));
			if(strtotime($value['send_time']) > 0) $value['send_time']  = date('Y-m-d',strtotime($value['send_time']));
			else $value['send_time']  = '';
			$value['start_time'] = date('Y-m-d',strtotime($value['start_time']));
			$value['end_time']   = date('Y-m-d',strtotime($value['end_time']));
			$value['contract_mount'] = floor($value['contract_mount']);
			//所属经理
			$manager_r['user_id'] = $value['sales_manager'];
			$manager = M("user")->field("nickname as manager_name")->where($manager_r)->find();
			$value['manager']   = $manager['manager_name'];
			//所属团队
			$team['sector_id']  = $value['sales_subcompany'];
			$sector = M("sector")->field("sector_name")->where($team)->find();
			$value['sector_name']   = $sector['sector_name'];
			//负责人
			$sales['user_id']  = $value['sales_id'];
			$user = M("user")->field("nickname")->where($sales)->find();
			$value['user_name']   = $user['nickname'];
		}
		if($list && !empty($list)){
			$res['data']['list']    = $list;
			$res['data']['page']    =  $Page->getTotalPages();
			$res['data']['p']       = $Page->nowPage;
			$res['data']['psize']   = $psize;
			$res['rcode']           = 1;
		}
		return $res;
	}
	
	//邮寄信息添加
	public function deliveryAdd($arr){
		$checkDelivery['source_id'] = I("post.contract"); 
		$deliveryContract = M("delivery")->where($checkDelivery)->find();
		if($arr['status'] == 10 && $deliveryContract){//待审核 只能编辑
			$map['recipients']       = $arr['recipients'];
			$map['phone']            = $arr['phone'];
			$map['address']          = $arr['address'];	
			$map['create_time']      = $arr['send_time']." ".date("H:i:s");
			$map['remark']           = $arr['remark'];
			$map['source_status']    = $arr['sign_status'];
			$map['status']           = 20;
			$find = M("delivery")->where($map)->find();
			if(!$find){
				$where['delivery_id']= $arr['delivery_id'];
				$delivery = M("delivery")->where($where)->save($map);
				if($delivery){
					$wheres['contract_id'] = $arr['contract'];
					
					//检测违规情况
					$fine = M("fine")->field("theme")->where($wheres)->find();
					if($fine && $fine['theme'] == 1) $maps['weigui_status'] = 21;
					
					$maps['sign_status'] = $arr['sign_status'];	
					$contract = M("contract")->where($wheres)->save($maps);
					$res['msg'] = "邮寄信息编辑成功!";
					$res['rcode'] = 1;
				}
			}else{
				$res['msg'] = "邮寄信息编辑成功!";
				$res['rcode'] = 1;
			}
		}else{
			$map['recipients']       = $arr['recipients'];
			$map['phone']            = $arr['phone'];
			$map['address']          = $arr['address'];	
			$map['customer_id']      = $arr['customer_id'];
			$map['remark']           = $arr['remark'];
			$map['sales_id']         = $arr['sales_id'];
			$map['sales_sector']     = $arr['sales_sector'];
			$map['sales_manager']    = $arr['sales_manager'];
			$map['sales_subcompany'] = $arr['sales_subcompany'];
			if(!empty($arr['send_time'])) $map['create_time'] = $arr['send_time']." ".date("H:i:s");
			else $map['create_time'] = date('Y-m-d H:i:s');
			if($arr['confirm_param']==1)
				 $map['status']           = 20;
			else
				 $map['status']           = 10;

			if($arr['invoice_id']){
				$map['source_type']  = 2;
				$map['source_id']    = $arr['invoice_id'];
			}else{
				$map['source_status']= $arr['sign_status'];	
				$map['source_type']  = 1;
				$map['source_id']    = $arr['contract'];
			}

			$list = M("delivery")->add($map);
			if($list){
				$res['rcode'] = 1;
				if($arr['invoice_id']){
					$res['delivery_id'] = $list;
					$res['invoice_id']  = $arr['invoice_id'];
				}else{
					$where['contract_id'] = $arr['contract'];
					
					//检测违规情况
					$fine = M("fine")->field("theme")->where($where)->find();
					if($fine && $fine['theme'] == 1) $maps['weigui_status'] = 21;
					
					$maps['sign_status'] = $arr['sign_status'];	
					$contract = M("contract")->where($where)->save($maps);
					$res['msg'] = "邮寄信息添加成功!";
				}	
			}else{
				$res['msg'] = "邮寄信息添加失败!";
			}
		}
		return $res;
	}
	//邮寄信息编辑
	public function deliveryEeditor($arr){
		$Delivery = M("delivery");
		$map['recipients']       = $arr['recipients'];
		$map['phone']            = $arr['phone'];
		$map['source_status']    = $arr['sign_status'];
		$map['address']          = $arr['address'];
		$map['create_time']      = $arr['send_time'];
		$map['remark']           = $arr['remark'];
		$where['delivery_id'] = $arr['delivery_id'];
		$list = $Delivery->where($where)->save($map);

		if($list || $list == 0){
			$res['rcode'] = 1;
			if($arr['contract_id']  ){
				$wheres['contract_id'] = $arr['contract_id'];
				$maps['sign_status'] = $arr['sign_status'];
				$contract = M("contract")->where($wheres)->save($maps);
			}else{
				$res['status'] = $arr['status'];
				if($arr['invoice_id']) $res['invoice_id'] = $arr['invoice_id'];
			}
		}else{
			$res['msg'] = "邮寄信息添加失败!";
		}
		return $res;
	}

	//邮寄导入
	public function import_delivery($arr){
		//所属团队
		$team['sector_name']      = $arr['company'];
		$company = M("sector")->field("sector_id")->where($team)->find(); 
		//所属经理id
		$sector['nickname']       = $arr['manager'];
		$manager =  M("user")->field("user_id,sector_id")->where($sector)->find();	
		if($arr['manager'] != $arr['sales']){
			//服务人员 
			$sales['nickname']        = $arr['sales'];
			$sales['manager_id']      = $manager['user_id']; 
			$user    = M("user")->field("user_id,sector_id")->where($sales)->find();
			$res['sector']   = $user['sector_id'];
			$res['sales']    = $user['user_id'];
		}else{
			$res['sector']   = $manager['sector_id'];
			$res['sales']    = $manager['user_id'];
		}	
		//客户
		$clientele['nickname']     = array('eq',$arr['customer']);
		$clientele['sales_person'] = array('eq',$res['sales']);
		$clientele['sales_sector'] = array('eq',$res['sector']);
		$customer = M("customer")->field("customer_id,sales_person")->where($clientele)->find();

		$res['company']  = $company['sector_id'];
		$res['manager']  = $manager['user_id'];
		$res['customer'] = $customer['customer_id'];
		return $res; 
	}
}